package com.foodorder.controller;

import com.foodorder.dto.ApiResponse;
import com.foodorder.service.DataSyncService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 数据同步测试控制器
 */
@RestController
@RequestMapping("/api/sync")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class DataSyncController {
    
    private final DataSyncService dataSyncService;
    
    /**
     * 测试数据同步推送
     */
    @PostMapping("/push")
    public ApiResponse<String> pushDataUpdate(
            @RequestParam String operation,
            @RequestParam String dataType,
            @RequestParam String targetRole,
            @RequestBody Object data) {
        try {
            dataSyncService.pushDataUpdate(operation, dataType, data, targetRole);
            return ApiResponse.success("数据同步推送成功");
        } catch (Exception e) {
            return ApiResponse.error("推送失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取最新同步数据
     */
    @GetMapping("/latest")
    public ApiResponse<Object> getLatestSyncData(
            @RequestParam String role,
            @RequestParam String dataType) {
        try {
            Object data = dataSyncService.getLatestSyncData(role, dataType);
            return ApiResponse.success(data);
        } catch (Exception e) {
            return ApiResponse.error("获取数据失败: " + e.getMessage());
        }
    }
    
    /**
     * 推送用户通知
     */
    @PostMapping("/notify")
    public ApiResponse<String> pushUserNotification(
            @RequestParam String userId,
            @RequestParam String role,
            @RequestBody Object data) {
        try {
            dataSyncService.pushToUser(userId, role, data);
            return ApiResponse.success("通知推送成功");
        } catch (Exception e) {
            return ApiResponse.error("推送失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取用户通知
     */
    @GetMapping("/notification")
    public ApiResponse<Object> getUserNotification(
            @RequestParam String userId,
            @RequestParam String role) {
        try {
            Object notification = dataSyncService.getUserNotification(userId, role);
            return ApiResponse.success(notification);
        } catch (Exception e) {
            return ApiResponse.error("获取通知失败: " + e.getMessage());
        }
    }
    
    /**
     * 广播消息
     */
    @PostMapping("/broadcast")
    public ApiResponse<String> broadcast(@RequestBody Object data) {
        try {
            dataSyncService.broadcast(data);
            return ApiResponse.success("广播成功");
        } catch (Exception e) {
            return ApiResponse.error("广播失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取广播消息
     */
    @GetMapping("/broadcast")
    public ApiResponse<Object> getBroadcastMessage() {
        try {
            Object message = dataSyncService.getBroadcastMessage();
            return ApiResponse.success(message);
        } catch (Exception e) {
            return ApiResponse.error("获取广播消息失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取同步统计信息
     */
    @GetMapping("/stats")
    public ApiResponse<Map<String, Object>> getSyncStats() {
        try {
            Map<String, Object> stats = dataSyncService.getSyncStats();
            return ApiResponse.success(stats);
        } catch (Exception e) {
            return ApiResponse.error("获取统计信息失败: " + e.getMessage());
        }
    }
    
    /**
     * 清理过期数据
     */
    @PostMapping("/cleanup")
    public ApiResponse<String> cleanupExpiredData() {
        try {
            dataSyncService.cleanExpiredData();
            return ApiResponse.success("清理完成");
        } catch (Exception e) {
            return ApiResponse.error("清理失败: " + e.getMessage());
        }
    }
} 